package cn.wolfcode.restful.controller;


import cn.wolfcode.restful.domain.Employee;
import cn.wolfcode.restful.util.JsonResult;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

//@Controller     //该注解表示：spring容器启动的时候，创建该对象，前提是：当前对象被扫描到
@RestController                // @ResponseBody + @Controller  == @RestController  表示该类中所有的处理方法都返回JSON格式的数据
@RequestMapping("/employees")
public class EmployeeController {

    @RequestMapping("/hello")
    public String hello(){
        return "hello restful";
    }


    @DeleteMapping
    public JsonResult delete(Long id){
        System.out.println(id+"删除操作");

        return  JsonResult.success(id);

    }


    @PutMapping
    public Employee update(Long id, String name,int age){

        //System.out.println("修改成功");
        Employee employee = new Employee();
        employee.setId(id);
        employee.setName(name+"更新后的数据");
        employee.setAge(age);
        return employee;

    }

    @GetMapping("/{id}")
    public Employee get(@PathVariable Long id){
        return new Employee(id,"芊芊",1);
    }

    //GET：查询所有员工
    @GetMapping
    public List<Employee> list(){

        //查询数据库，这里假装查询了数据库
        return  Arrays.asList(new Employee(1L,"小芳",22)
                ,new Employee(2L,"大芳",25));

    }



    @GetMapping("/{id}/{name}/{age}")
    public Employee get(Employee employee){

        //假装到数据库查询员工信息了
        return employee;

    }

    @PostMapping
    public Employee save(Employee employee){
        //添加操作
        employee.setId(7L);
        return employee;

    }




}
